package com.example.myapp.activities.show_bills_by_months;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.example.myapp.R;
import com.example.myapp.activities.main_activity.MainActivity;
import com.example.myapp.bill.Bill;
import com.example.myapp.database.BillDao;

import java.util.List;

public class ShowBillsByMonth extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener{
    private StabledArrayAdapter adapter;
    private Button powrotBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUp();
        initComponents();
    }

    private void setUp() {
        setContentView(R.layout.show_bills_by_month);
        setTitle("Opłacone rachunki");
    }

    private void initComponents() {
        BillDao billDao = new BillDao(this);

        if (getIntent().getExtras() != null) {
            String data = String.valueOf(getIntent().getExtras().get("data"));

            powrotBtn = (Button) findViewById(R.id.powrotBtn);
            powrotBtn.setOnClickListener(this);

            TextView miesiacTextView = (TextView) findViewById(R.id.miesiac);
            ListView oplaconeRachListView = (ListView) findViewById(R.id.rachunkiMiesiacListView);
            oplaconeRachListView.setOnItemClickListener(this);

            List<Bill> paidBillsForMonth = billDao.getPaidBillsForMonth(data);

            adapter = new StabledArrayAdapter(this, paidBillsForMonth);
            oplaconeRachListView.setAdapter(adapter);

            if (paidBillsForMonth.size() > 0) {
                miesiacTextView.setText("Opłacone rachunki w miesiącu " + data);
            } else {
                miesiacTextView.setText("Brak opłaconych rachunków w tym miesiącu");
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.rachunkiMiesiacListView:
                adapter.setSelected(position);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.powrotBtn:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
        }
    }
}
